//
//  ViewController.m
//  MrLvSegment
//
//  Created by tgs on 2016/12/9.
//  Copyright © 2016年 tgs. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "MrLvSegmentView.h"


#define SCREEN_WIDTH  [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()<MrLvSegmentViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) MrLvSegmentView *segmentView;
@property (nonatomic, strong) UIScrollView    *mainScrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self stepUI];
}

- (void)stepUI{
    
    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"红色",@"蓝色",@"绿色", nil];
    NSMutableArray *arrImage = [[NSMutableArray alloc] initWithObjects:@"icon_subtab_list_act",@"icon_subtab_info_act",@"icon_subtab_review_act",nil];
    
    _segmentView = [[MrLvSegmentView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 50) withTitlesArr:arr iconArr:arrImage];
//    _segmentView.lineView.hidden = YES;
    _segmentView.delegate = self;
    [self.view addSubview:_segmentView];
    
    
    
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.delegate = self;
    
    _mainScrollView.pagingEnabled = YES;
    [self.view addSubview:_mainScrollView];
    [_mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).mas_offset(70);
    }];
    
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [_mainScrollView addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(_mainScrollView);
        make.height.equalTo(@(SCREEN_HEIGHT-70));
        make.width.equalTo(@(SCREEN_WIDTH));
    }];
    
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [_mainScrollView addSubview:blueView];
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainScrollView);
        make.left.equalTo(redView.mas_right);
         make.height.equalTo(@(SCREEN_HEIGHT-70));
        make.width.equalTo(@(SCREEN_WIDTH));
    }];
    
    UIView *greenView = [[UIView alloc] init];
    greenView.backgroundColor = [UIColor greenColor];
    [_mainScrollView addSubview:greenView];
    [greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mainScrollView);
        make.left.equalTo(blueView.mas_right);
        make.height.equalTo(@(SCREEN_HEIGHT-70));
        make.width.equalTo(@(SCREEN_WIDTH));
        make.right.equalTo(_mainScrollView.mas_right);
    }];
    
}
#pragma mark-MrLvSegmentDelegate
- (void)selectedIndex:(NSInteger)index {
     [self.segmentView selectIndex:index];
    [_mainScrollView setContentOffset:CGPointMake(index*SCREEN_WIDTH, 0) animated:YES];
}

#pragma mark-ScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x/SCREEN_WIDTH;
    [self.segmentView selectIndex:page];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
